package com.annis.myviewpager

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import android.view.Window
import android.widget.*
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    val imgs = arrayListOf(
        R.mipmap.a1, R.mipmap.a2, R.mipmap.a3, R.mipmap.a4, R.mipmap.a5, R.mipmap.a6
    )

    override fun onCreate(savedInstanceState: Bundle?) {
        //方式一：这句代码必须写在setContentView()方法的后面
//        supportActionBar?.hide()
        //方式二：这句代码必须写在setContentView()方法的前面
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE)

        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        imgs.forEach {
            var imageView = ImageView(this@MainActivity)
            imageView.setBackgroundResource(it)
            viewPager.addView(imageView)
        }
        val view = LayoutInflater.from(this).inflate(R.layout.myview, null)
        view.findViewById<TextView>(R.id.textView).setOnClickListener {
            Toast.makeText(this@MainActivity, "TextView be clicked", Toast.LENGTH_SHORT).show()
        }
        view.findViewById<ImageView>(R.id.pb).setOnClickListener {
            Toast.makeText(this@MainActivity, "ProgressBar be clicked", Toast.LENGTH_SHORT).show()
        }
        val params = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        viewPager.addView(view, 2, params)
        for (i in 0 until viewPager.childCount) {
            val radioButton = RadioButton(this)
            radioButton.id = i
            if (i == 0)
                radioButton.isChecked = true
            pagerTable.addView(radioButton)
        }
        viewPager.mPageChanger = object : MyViewPager.PageChanger {
            override fun onPageChanged(page: Int) {
                pagerTable.check(page)
            }
        }
        pagerTable.setOnCheckedChangeListener { group, checkedId ->
            viewPager.scrollToPage(checkedId)
        }
    }
}